package edu.hitsz;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class calculator_demo {
    private JPanel main;
    private JPanel top;
    private JPanel button;
    private JPanel test2;
    private JTextField num1field;
    private JLabel num1label;
    private JTextField num2field;
    private JTextField resultfield;
    private JLabel num2label;
    private JLabel resultlabel;
    private JButton add;
    private JButton sub;
    private JButton mul;
    private JButton clear;

    public calculator_demo() {
    num1field.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String num1 = num1field.getText();
                String num2 = num2field.getText();
                double result = Double.parseDouble(num1) + Double.parseDouble(num2);
                resultfield.setText(String.valueOf(result));
            }
        });
        sub.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String num1 = num1field.getText();
                String num2 = num2field.getText();
                double result = Double.parseDouble(num1) - Double.parseDouble(num2);
                resultfield.setText(String.valueOf(result));
            }
        });
        mul.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String num1 = num1field.getText();
                String num2 = num2field.getText();
                double result = Double.parseDouble(num1) * Double.parseDouble(num2);
                resultfield.setText(String.valueOf(result));
            }
        });
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                num1field.setText("");
                num2field.setText("");
                resultfield.setText("");
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("test");
        frame.setContentPane(new calculator_demo().main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
